package com.htj.powerbank.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.htj.powerbank.entity.User;
import com.htj.powerbank.service.UserService;
import com.htj.powerbank.vo.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    UserService userService;

    @GetMapping("/list")
    public R Adminlist(@RequestParam("pageNum") int pageNum,
                   @RequestParam("pageSize") int pageSize,
                   @RequestParam("sort") String sort,
                   User user) {
        Boolean isDesc = new Boolean(sort.equals("+id"));
        PageRequest pageQuery = new PageRequest();
        pageQuery.setPageNum(pageNum);
        pageQuery.setPageSize(pageSize);
        return R.ok(userService.selectUserInfoListBy(pageQuery,user));
    }

    @PostMapping("/create")
    public R Admincreate(@RequestBody User user){
        JSONObject jsonObject = new JSONObject();
//        System.out.println(user);
        if(user.getName() != null && user.getPassword() != null) {

            //通过用户名查询该用户
            User one = userService.getUserByName(user.getName());
//            System.out.println(one);
            //用户存在
            if(one != null) {
                jsonObject.put("code",201);
                jsonObject.put("msg","注册失败，用户名已存在");
                return R.ok(jsonObject);
            }
            else
            {
                int check = userService.addUser(user);
                System.out.println(check);
                if(check==1) {
                    jsonObject.put("code", 200);
                    jsonObject.put("msg","注册成功");
                    return R.ok(jsonObject);
                }
            }
        }

        jsonObject.put("code", 202);
        jsonObject.put("msg", "其他错误");
        return R.ok(jsonObject);
    }

    @PostMapping("/update")
    public R Adminupdate(@RequestBody User user){
        JSONObject jsonObject = new JSONObject();
        int check = userService.updateUser(user);
        System.out.println(check);
        if(check==1) {
            jsonObject.put("code", 200);
            jsonObject.put("msg","更新成功");
            return R.ok(jsonObject);
        }
        jsonObject.put("code", 201);
        jsonObject.put("msg", "更新错误");
        return R.ok(jsonObject);
    }

    @PostMapping("/updatestatus")
    public R AdminupdateStatus(@RequestBody User user){
        JSONObject jsonObject = new JSONObject();
        int check = userService.updateUserStatus(user.getId(),user.getStatus());
        System.out.println(check);
        if(check==1) {
            jsonObject.put("code", 200);
            jsonObject.put("msg","更新成功");
            return R.ok(jsonObject);
        }
        jsonObject.put("code", 201);
        jsonObject.put("msg", "更新错误");
        return R.ok(jsonObject);
    }
}
